package com.tony.juc;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: ThreadPoolExecutorDemo001
 * @Auther: 皮蛋布丁
 * @Date: 2022/04/17/20:38
 * @Description: 阻塞队列
 */
public class ThreadPoolExecutorDemo001 {

    public static void main(String[] args) {
        /*BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1); //阻塞队列
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,2,1, TimeUnit.SECONDS,queue,
                Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < 5; i++) {
            final int temp = i;
            executor.submit(()->{
                System.out.printf("【%s】处理皮蛋布丁任务，x = %d%n",Thread.currentThread().getName(),temp); //线程任务处理
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executor.shutdownNow();*/

        //直接中断拒绝
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1); //阻塞队列
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,2,1, TimeUnit.SECONDS,queue,
                Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 5; i++) {
            final int temp = i;
            executor.submit(()->{
                System.out.printf("【%s】处理皮蛋布丁任务，x = %d%n",Thread.currentThread().getName(),temp); //线程任务处理
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executor.shutdownNow();
    }
}
